package com.ruimei.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

public class AliyunOSSClientUtil
{
  private static String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
  private static String ACCESS_KEY_ID = "LTAIWWcOb0IvnSEr";
  private static String ACCESS_KEY_SECRET = "2repsLXCWlQLSEES2qpRk8US0nSJnq";
  private static String BACKET_NAME = "sagiri";
  private static String FOLDER = "teacher/";
  
  public static OSSClient getOSSClient()
  {
    return new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
  }
  
  public static String createBucketName(OSSClient ossClient, String bucketName)
  {
    String bucketNames = bucketName;
    if (!ossClient.doesBucketExist(bucketName))
    {
      Bucket bucket = ossClient.createBucket(bucketName);
      
      return bucket.getName();
    }
    return bucketNames;
  }
  
  public static void deleteBucket(OSSClient ossClient, String bucketName)
  {
    ossClient.deleteBucket(bucketName);
  }
  
  public static String createFolder(OSSClient ossClient, String bucketName, String folder)
  {
    String keySuffixWithSlash = folder;
    if (!ossClient.doesObjectExist(bucketName, keySuffixWithSlash))
    {
      ossClient.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
      
      OSSObject object = ossClient.getObject(bucketName, keySuffixWithSlash);
      String fileDir = object.getKey();
      return fileDir;
    }
    return keySuffixWithSlash;
  }
  
  public static void deleteFile(OSSClient ossClient, String bucketName, String folder, String key)
  {
    ossClient.deleteObject(bucketName, folder + key);
  }
  
  public static String uploadObject2OSS(OSSClient ossClient, File file, String bucketName, String folder)
  {
    String resultStr = null;
    String fileName = null;
    try
    {
      InputStream is = new FileInputStream(file);
      
      fileName = UUID.randomUUID() + file.getName();
      
      Long fileSize = Long.valueOf(file.length());
      
      ObjectMetadata metadata = new ObjectMetadata();
      
      metadata.setContentLength(is.available());
      
      metadata.setCacheControl("no-cache");
      
      metadata.setHeader("Pragma", "no-cache");
      
      metadata.setContentEncoding("utf-8");
      
      metadata.setContentType(getContentType(fileName));
      
      metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte.");
      
      PutObjectResult putResult = ossClient.putObject(bucketName, folder + fileName, is, metadata);
      
      resultStr = putResult.getETag();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return "https://sagiri.oss-cn-beijing.aliyuncs.com/" + folder + fileName;
  }
  
  public static String getContentType(String fileName)
  {
    String fileExtension = fileName.substring(fileName.lastIndexOf("."));
    if (".bmp".equalsIgnoreCase(fileExtension)) {
      return "image/bmp";
    }
    if (".gif".equalsIgnoreCase(fileExtension)) {
      return "image/gif";
    }
    if ((".jpeg".equalsIgnoreCase(fileExtension)) || (".jpg".equalsIgnoreCase(fileExtension)) || (".png".equalsIgnoreCase(fileExtension))) {
      return "image/jpeg";
    }
    if (".html".equalsIgnoreCase(fileExtension)) {
      return "text/html";
    }
    if (".txt".equalsIgnoreCase(fileExtension)) {
      return "text/plain";
    }
    if (".vsd".equalsIgnoreCase(fileExtension)) {
      return "application/vnd.visio";
    }
    if ((".ppt".equalsIgnoreCase(fileExtension)) || ("pptx".equalsIgnoreCase(fileExtension))) {
      return "application/vnd.ms-powerpoint";
    }
    if ((".doc".equalsIgnoreCase(fileExtension)) || ("docx".equalsIgnoreCase(fileExtension))) {
      return "application/msword";
    }
    if (".xml".equalsIgnoreCase(fileExtension)) {
      return "text/xml";
    }
    return "image/jpeg";
  }
  
  public static void main(String[] args)
  {
    OSSClient ossClient = getOSSClient();
    
    String files = "D:\\image\\1010.jpg,D:\\image\\1111.jpg,D:\\image\\1212.jpg,D:\\image\\1313.jpg,D:\\image\\2222.jpg,D:\\image\\3333.jpg,D:\\image\\4444.jpg,D:\\image\\5555.jpg,D:\\image\\6666.jpg,D:\\image\\7777.jpg,D:\\image\\8888.jpg";
    
    String[] file = files.split(",");
    for (String filename : file)
    {
      File filess = new File(filename);
      String str1 = uploadObject2OSS(ossClient, filess, BACKET_NAME, FOLDER);
    }
  }
}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruime\\util\AliyunOSSClientUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */